// ----------------------------
const {jpPrinter} = require('./esc')
const command = jpPrinter.createNew()
let _WIDTH_ = 32
// 初始化打印机 设置打印机型号  58 打印机 32字符 80 打印机 48字符 & 打印测试
const printerInit = (type) => {
  switch (type) {
    case 58:
      _WIDTH_ = 32
      break
    case 80:
      _WIDTH_ = 48
      break
    default:
      _WIDTH_ = 32
  }
  command.init()
  // 打印测试
  // command.setFontSize(1, 1)
  // command.setSelectJustification(48)
  // command.setBold(true)
  // command.setText('打印测试')
  // command.setPrint()
  // command.setBold(false)
  // command.setText('deviceId(蓝牙设备ID)')
  // command.setPrint()
  // command.setText(deviceId)
  // command.setPrint()
  // command.setText('characteristicId(蓝牙特征值)')
  // command.setPrint()
  // command.setText(characteristicId)
  // command.setPrint()
  // if (systemInfo && systemInfo.errMsg === 'getSystemInfoSync:ok') {
  //   command.setText('当前连接设备参数：')
  //   command.setPrint()
  //   command.setText(`设备型号：${systemInfo.brand} ${systemInfo.model}`)
  //   command.setPrint()
  //   command.setText(`系统版本：${systemInfo.platform} ${systemInfo.system}(${systemInfo.language})`)
  //   command.setPrint()
  // }
  // command.setPrintAndFeedRow(3)
  return command
}

// 获取时间戳转时间
const timestampToTime = (timestamp, type = ['-', ':']) => {
  const date = new Date(timestamp)
  const y = date.getFullYear()
  const m = date.getMonth() + 1
  const d = date.getDate()
  const H = date.getHours()
  const M = date.getMinutes()
  const S = date.getSeconds()
  return `${[y, _addZero(m), _addZero(d)].join(type[0])} ${[_addZero(H), _addZero(M), _addZero(S)].join(type[1])}`
}

// 小于10补0
const _addZero = (num) => {
  num = parseInt(num)
  return num < 10 ? `0${num}` : num
}

const strlen = (str) => {
  let len = 0
  for (let i = 0; i < str.length; i++) {
    let c = str.charCodeAt(i)
    //单字节加1
    if ((c >= 0x0001 && c <= 0x007e) || (0xff60 <= c && c <= 0xff9f)) {
      len++
    } else {
      len += 2
    }
  }
  return len
}
// 获取字符长度 中文=2 英文=1
const getLength = (text) => {
  return strlen(text)
}

// 生成特殊符号 一个汉字宽度 ※ └
const generateSpacialSymbol = (len, symbol) => {
  return new Array(len).fill(symbol).join('')
}

// 获取标题
const getTitle = (title) => {
  let _title = `序号 ${title} `
  let l = getLength(_title)
  let b = parseInt((_WIDTH_ - l) / 2)
  let a = _WIDTH_ - l - b
  return generateSpacialSymbol(a, '-') + _title + generateSpacialSymbol(b, '-')
}
// 获取商品标题
const getGoodsTitle = (_title) => {
  let l = getLength(_title)
  let b = parseInt(((_WIDTH_ / 2) - l) / 2)
  let a = (_WIDTH_ / 2) - l - b
  return generateSpacialSymbol(a, '※') + _title + generateSpacialSymbol(b, '※')
}

// 商品说明
const foodFirst = () => {
  return '名称' + generateSpacialSymbol((_WIDTH_ - 26), ' ') + fillBlank('单价', 7) + generateSpacialSymbol(2, ' ') + '数量' +
    generateSpacialSymbol(2, ' ') + fillBlankFont('金额', 7)
}

// 菜品换行
const goodWrap = (good) => {
  let singlePrice = fillBlank(good.price, 7)
  let number = fillBlank(good.number, 4)
  let totalPrice = fillBlankFont((good.number * good.price).toString(), 7)
  return generateSpacialSymbol((_WIDTH_ - 26), ' ') + singlePrice + generateSpacialSymbol(2, ' ') + number + generateSpacialSymbol(
    2, ' ') + totalPrice
}
// 菜品不换行
const goodNoWrap = (good) => {
  let title = fillBlank(good.title, (_WIDTH_ - 26))
  let singlePrice = fillBlank(good.price, 7)
  let number = fillBlankFont(good.number, 4)
  let totalPrice = fillBlankFont((good.number * good.price).toString(), 7)
  return title + singlePrice + generateSpacialSymbol(2, ' ') + number + generateSpacialSymbol(
    2, ' ') + totalPrice
}
// 打印商品
const printerGoods = (goodList) => {
  goodList.forEach(item => {
    const len = getLength(item.title)
    if (len > (_WIDTH_ - 26)) {
      // 换行
      command.setText(item.title)
      command.setPrint()
      command.setText(goodWrap(item))
      command.setPrint()
    } else {
      // 不换行
      command.setText(goodNoWrap(item))
      command.setPrint()
    }
  })
}

// 后面补空格
const fillBlank = (str, len) => {
  return str + generateSpacialSymbol(len - getLength(str), ' ')
}
// 前面补空格
const fillBlankFont = (str, len) => {
  return generateSpacialSymbol(len - getLength(str), ' ') + str
}

module.exports = {
  printerInit,
  getLength,
  getTitle,
  timestampToTime,
  getGoodsTitle,
  generateSpacialSymbol,
  foodFirst,
  printerGoods,
}
